﻿


using System;
using Demo.Infrastructure;
using Demo.Domain;
using Demo.ServiceContracts;
using Demo.Repository;
using Demo.DataObjects;
using AutoMapper;

namespace Demo.Application
{
    public class UserServiceImpl : IUserService 
    {
        private readonly IUserRepository userRepository = ServiceLocator.Instance.GetService<IUserRepository>();

        public UserDTO GetUserDTOWithName(string userName)
        {
            var user = userRepository.GetByName(userName);
            //if (user == null)
            //throw new Exception("异常：测试");
            return Mapper.Map<User, UserDTO>(user);
            //throw new Exception("异常：测试");
        }

        public bool ExistsWithUserName(string userName)
        {
            return userRepository.ExistsWithUserName(userName);
        }

        public bool Exists(string userName, string password)
        {
            return userRepository.Exists(userName, password);
        }

        public void CreateUser(string userName, string password, int roleID)
        {
            userRepository.CreateUser(userName, password, roleID);
        }

        public bool ChangePassword(string userName, string password, string newPassword)
        {
            return userRepository.ChangePassword( userName,  password,  newPassword);
        }

        public bool ChangeRole(string userName, int roleID)
        {
            return userRepository.ChangeRole(userName,roleID);
        }
    }
}
